/** 
 * Copyright (C), 2010-2012, AIYOU Software Co.,Ltd. ALL RIGHTS RESERVED. 
 * File name:UserLoginInterceptor.java
 * Version:1.0
 * Date:2011-12-26
 * Description: 
 * author:whh 
 */
package com.tietiao.filter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.tietiao.common.bean.ThreadLocalBean;
import com.tietiao.common.constants.SystemContext;
import com.tietiao.common.util.HttpServletUtil;
import com.tietiao.common.util.TietiaoUtil;
import com.tietiao.dao.model.SysUser;

public class UserLoginInterceptor extends AbstractInterceptor {
	private static final long serialVersionUID = 892629017475606461L;

	public String intercept(ActionInvocation invocation) throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		TietiaoUtil.setRootPath(request);

		SysUser sysUser = TietiaoUtil.getSessionUser(request);
		String uri = request.getRequestURI();
		if (sysUser == null) {
			TietiaoUtil.cookiesLogin(request, response);
			sysUser = TietiaoUtil.getSessionUser(request);
		}
		if(uri.startsWith("/tietiao/bg_")) {
			if(uri.equals("/tietiao/bg_bguser_loginIndex.htm") || uri.equals("/tietiao/bg_bguser_login.htm")) {
				return invocation.invoke();
			}
			if(sysUser == null || sysUser.getType() == 0) {
				return "bg_login";
			}
			return invocation.invoke();
		}
		if (uri.startsWith("/tietiao/member_") 
//				|| uri.startsWith("/tietiao/index") 
				|| uri.startsWith("/tietiao/index_commodity_index.htm")
				|| uri.startsWith("/tietiao/index_commodity_lableInfo.htm")
				|| uri.startsWith("/tietiao/index_commodity_loadCommodityInfo.htm")) {
			return invocation.invoke();
		} else if (sysUser != null) {
			ThreadLocalBean threadLocalBean = new ThreadLocalBean();
			threadLocalBean.setSysUser(sysUser);
			threadLocalBean.setUserIp(HttpServletUtil
					.getRealRemoteAddr(request));
			SystemContext.setThreadLocalBean(threadLocalBean);
			return invocation.invoke();
		} else {
			return Action.LOGIN;
		}
	}
}
